<?php

namespace iBrand\Shop\Core\Repositories;

use iBrand\Shop\Core\Models\CashRecord;
use Prettus\Repository\Eloquent\BaseRepository;

class CashRecordRepository extends BaseRepository
{
	public function model()
	{
		return CashRecord::class;
	}

	/**
	 * @param array $where
	 * @param array $time
	 * @param int   $limit
	 *
	 * @return mixed
	 */
	public function getCashRecordPaginate(array $where, array $time = [], $limit = 15)
	{
		$query = $this->scopeQuery(function ($query) use ($where, $time) {
			if (!empty($where)) {
				foreach ($where as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			if (!empty($time)) {
				foreach ($time as $key => $value) {
					if (is_array($value)) {
						list($operate, $va) = $value;
						$query = $query->where($key, $operate, $va);
					} else {
						$query = $query->where($key, $value);
					}
				}
			}

			return $query->orderBy('created_at', 'DESC');
		});

		if ($limit) {
			return $query->paginate($limit);
		} else {
			return $query->all();
		}
	}
}